﻿using NPOI.SS.UserModel;
using System;
using System.ComponentModel;
using System.IO;

namespace XLSranker
{
    class DeleteRow
    {
        public static void deleteRow(IWorkbook workbook, BackgroundWorker worker)
        {
            ISheet sheet = workbook.GetSheetAt(0);
            //int lastRowNum = sheet.LastRowNum;
            int totalNoNullNum = 0;
            int totalGroupNum = 0;
            float fl = 0; //进度计数变量

            //计算出所有非空行数
            for (int a = 0; a <= sheet.LastRowNum; a++)
            {
                if (sheet.GetRow(a).GetCell(0) != null)
                {
                    if (sheet.GetRow(a).GetCell(0).ToString() != "")
                        totalNoNullNum++;
                }
            }

            //计算出串号组数
            for (int a = 0; a <= totalNoNullNum - 1; a++)
            {
                if (sheet.GetRow(a).GetCell(2) != null)
                {
                    //if (sheet.GetRow(a).GetCell(0).ToString() != "")
                        totalGroupNum++;
                }
            }

            //Console.WriteLine("indel,sheet.LastRowNum={0}", sheet.LastRowNum);
            //Console.WriteLine("indel,totalNoNullNum={0}", totalNoNullNum);
            //Console.WriteLine("indel,totalGroupNum={0}", totalGroupNum);

            if (totalGroupNum == 1)
            {
                for (int rowNum = 1; rowNum < totalNoNullNum - 1; rowNum++)
                {
                    sheet.RemoveRow(sheet.GetRow(rowNum));
                    fl = ((float)rowNum / (totalNoNullNum - 1));
                    worker.ReportProgress((int)(fl * 100));
                }
            }
            else
            {
                for (int i = 0; i <= totalGroupNum - 1; i++)
                {
                    //Console.WriteLine(i);
                    fl = ((float)i / (totalGroupNum - 1)); //fl用于存储当前进行到的进度数据
                    //Console.WriteLine(fl);
                    worker.ReportProgress((int)(fl * 100));
                    if (sheet.GetRow(i).GetCell(2) != null)
                    {
                        if (i < totalGroupNum - 1)
                        {
                            int n = (int)sheet.GetRow(i).GetCell(2).NumericCellValue; //取得串号计数

                            if (n == 1)
                            {
                                continue;
                            }
                            else
                            {
                                for (int rowNum = i + 1; rowNum < i + n; rowNum++)
                                {
                                    sheet.RemoveRow(sheet.GetRow(rowNum));
                                }
                                sheet.ShiftRows(i + n, totalNoNullNum - 1, -(n - 1));//表格整体上移(串号计数-1)行，以便删除空行
                                totalNoNullNum = totalNoNullNum - (n - 1);
                            }
                        }
                        else
                        {
                            int n = (int)sheet.GetRow(i).GetCell(2).NumericCellValue; //取得串号计数                       
                            if (n == 1)
                            {
                                continue;
                            }
                            else
                            {
                                for (int rowNum = i + 1; rowNum < i + n; rowNum++)
                                {
                                    sheet.RemoveRow(sheet.GetRow(rowNum));
                                }
                            }
                        }
                    }
                    else continue;
                }
            }            
        }
    }
}